package mk.company.fin.importer;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import mk.company.fin.model.KontoClass;
import mk.company.fin.model.Organization;
import mk.company.fin.model.User;
import mk.company.fin.service.KontoService;
import mk.company.fin.service.ModelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class KontoImporter extends AbstractImporter {
	private static final int COLUMNSCOUNT = 2;
	
	@Autowired
	@Qualifier("kontoService")
	private KontoService kontoService;
	@Autowired
	private ModelService modelService;
	
	protected KontoImporter() {
		super(COLUMNSCOUNT);
//		if (modelService.findAll(KontoClass.class)==null||modelService.findAll(KontoClass.class).size()==0){
//			System.out.println("Importing Data");
//		}
	}
	
	public void importKontos(){
		if (kontoService!=null&&modelService!=null){
			try {
				if (modelService.findAll(KontoClass.class)==null||modelService.findAll(KontoClass.class).size()==0){
					InputStream inputStream=new FileInputStream(new File("src/core/resources/files/konts-synthetic.csv"));
					importContent(inputStream);
				}
			}
			catch (Exception e){
				System.out.println(e.getMessage());
			}
			try {
				if (modelService.findAll(Organization.class)==null||modelService.findAll(Organization.class).size()==0){
					Organization organization=new Organization();
					organization.setName("JP Ulici i Patista");
					modelService.save(organization);
				}
			}
			catch (Exception e){
				
			}
			
		}
		
		
	}
	
	public void importFirstUser(){
		try {
			if (modelService.findAll(User.class)==null||modelService.findAll(User.class).size()==0){
				User user=new User();
				user.setFirstName("Admin");
				user.setLastName("Admin");
				user.setUsername("admin");
				user.setPassword("JPUPadmin");
				user.setLanguage("mk");
				user.setActive(true);
				modelService.update(user);
			}
		}
		catch (Exception ex){
			System.out.println(ex.getMessage());
		}
	}
	@Override
	protected String importLine(String... contents) {
		return kontoService.saveKonto(contents[0], contents[1]);
	}

}
